function save() {

    let data = {
        id: sessionStorage.getItem("userId"),
        username: $("#username").val(),
        sex: $("input[type=radio][name=sex]:checked").val(),
        birthday: $("#birthday").val(),
        height: $("#height").val(),
        email: $("#email").val(),
        hobby: getHobby()
    };
    let result = myAjax("/back/user/update", data, "post");
    if (result.result == 1) {
        closeEditWindow();
        $("#right").load("/html/back/userManage.html");
    } else {
        alert("修改失败");
    }
}

function getHobby() {
    let hobby = '';
    $('input:checkbox[name=hobby]').each(function () {
        if ($(this).attr('checked') == "checked") {
            hobby += $(this).val() + ",";
        }
    });
    return hobby;
}

$(function () {
    findById();
})

function findById() {
    let data = {id: sessionStorage.getItem("userId")};
    let result = myAjax('/back/user/findById', data, "get");
    console.log(result);
    setData(result.model);
    // $.ajax({
    //     url: '/back/user/findById',
    //     data: data,
    //     type: 'get',
    //     dataType: 'json',
    //     success: function (data) {
    //         setData(data.model);
    //     }
    // })
}

function setData(data) {
    $("#username").val(data.username);
    // $("input[name=sex][value=" + data.sex + "]:checked").attr(true);
    $("input[name='sex'][value=" + data.sex + "]").attr("checked", true);
    $("#birthday").val(data.birthday);
    $("#height").val(data.height);
    $("#email").val(data.email);
    setHobby(String(data.hobby));
}

function setHobby(data) {
    let hobby = data.split(",");
    for (let i = 0; i < hobby.length; i++) {
        $('input:checkbox[name=hobby]').each(function () {
            if ($(this).val() == $.trim(hobby[i])) {
                $(this).attr('checked', true);
            }
        });
    }

}